package Controller;

import Pojo.Student;

import Service.ServiceImpl.StudentServiceImpl;
import Service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @auther wsy
 * @auther MI
 * @create Servlet_Jdbc_Maven.${PACKAGE_NAME}
 * @create 6:11
 * @create 2021/7/6 星期二
 */
@WebServlet("/register")
public class InsertNewUser extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String gender=request.getParameter("gender");
        Integer age=Integer.valueOf(request.getParameter("age"));
        String address=request.getParameter("address");
        Integer group=Integer.valueOf(request.getParameter("group"));
        String classname=request.getParameter("classname");

        Student s = new Student();
        s.setName(username);
        s.setPassword(password);
        s.setGender(gender);
        s.setAge(age);
        s.setAddress(address);
        s.setGroup(group);
        s.setClassname(classname);
        System.out.println(s);

        StudentService studentService = new StudentServiceImpl();
        int count= studentService.InsertNewUser(s);

        if(count>0){
            request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        }
        else{
            request.getRequestDispatcher("/admin/register.jsp").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
